<template>
  <div>
    <el-dialog :title="info.isAdd ? '添加' : '编辑'" :visible.sync="info.isShow" @close="close">
      <div>{{ user }}</div>
      <el-form :model="user" label-width="80px">
        <el-form-item label="所属角色">
          <el-select v-model="user.roleid" placeholder="请选择">
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="user.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-if="info.isAdd" type="primary" @click="add"
          >添 加</el-button
        >
        <el-button v-else type="primary" @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {
  reqrolelist,
  requseradd,
  requserinfo,
  requseredit,
} from "../../../http/api";
import { successAlert } from "../../../utils/alert";
import { formClose } from "../../../mixins/index";
export default {
  mixins: [formClose],
  props: ["info"],
  data() {
    return {
      user: {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      },
      //角色列表
      roleList: [],
    };
  },
  computed: {
    ...mapGetters({ userInfo: "userInfo" }),
  },
  mounted() {
    this.getroleData();
  },
  methods: {
    ...mapActions({ chageUserInfo: "changeUserInfo" }),
    //获取角色列表
    async getroleData() {
      let { data: res } = await reqrolelist();
      if (res.code == 200) {
        this.roleList = res.list ? res.list : [];
        console.log(this.roleList);
      }
    },
    //清空表单
    empty() {
      this.user = {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      };
    },
    //获取一条用户信息
    async getOne(id) {
      let { data: res } = await requserinfo({ uid: id });
      if (res.code == 200) {
        this.user = res.list;
        this.user.password = "";
      }
    },
    //添加
    async add() {
      let { data: res } = await requseradd(this.user);
      if (res.code == 200) {
        successAlert(res.msg);
        //添加成功关闭弹窗
        this.info.isShow = false;
        //清空数据
        this.empty();
        //重新请求列表页数据
        this.$emit("init");
      }
    },
    //修改
    async update() {
      let { data: res } = await requseredit(this.user);
      if (res.code == 200) {
        successAlert(res.msg);
        //判断是否修改了此角色的信息
        if (this.user.uid == this.userInfo.uid) {
          this.changeUserInfo({});
          this.$router.push("/login");
          return;
        }
        //添加成功关闭弹窗
        this.info.isShow = false;
        //清空数据
        this.empty();
        //重新请求列表页数据
        this.$emit("init");
      }
    },
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>